package toy.keli.edic.ui.home.game

import android.content.Context.WINDOW_SERVICE
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.graphics.Point
import android.view.LayoutInflater
import android.view.OrientationEventListener
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Button
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import okhttp3.Cache.Companion.key
import toy.keli.edic.MainActivity
import toy.keli.edic.R
import toy.keli.edic.ui.home.HomeEvents
import toy.keli.edic.ui.home.game.ext.LineGame
import toy.keli.edic.ui.main.components.Display
import toy.keli.edic.ui.main.components.DotPage
import toy.keli.edic.ui.main.extend.setTimeout
import toy.keli.edic.util.ActivityRun
import toy.keli.edic.util.ScreenUtil
import kotlin.math.abs

@Composable
fun LineGameScreen(words: List<String>) {
    val act = ActivityRun.context as MainActivity
    var orientation = act.resources.configuration.orientation

    val layoutId = remember { mutableStateOf(getLayout(orientation)) }
    val lg = remember {
        ActivityRun.onOrientationChange{ orientation->
            layoutId.value=getLayout(orientation);
        }
        LineGame(words)
    }
    var nowPage = remember { mutableStateOf(0) }

    Column  {
        //添加分页
        if(layoutId.value ==  R.layout.activity_wordsearch_game_old) {
            DotPage(lg.pageSize, modifier = Modifier.height(300.dp)
                .weight(1.2f)
                .padding(10.dp)
            ,onChagePage = {
                nowPage.value = it
            }){
                LazyColumn { itemsIndexed(lg.args){ i,word->
                    Text((lg.pageIndex*10+i+1).toString()+". "+ lg.names.get(word),modifier=Modifier.clickable {
                        setTimeout(500,4){
                            lg.wordSearchView.show(word)
                        }
                    })
                }}
            }
        }
        key(nowPage.value) {
            AndroidView(
                factory = {
                    //加载AndroidView布局。
                    LayoutInflater.from(it).inflate(layoutId.value, null)
                },
                modifier = Modifier
                    .weight(2f),
            ) { it ->
                lg.initView(it, nowPage.value)
            }
        }
    }
}


fun getLayout(orientation: Int): Int {
    val act = ActivityRun.context as MainActivity
    var orientation = orientation
    val point = Point()
    (act.getSystemService(WINDOW_SERVICE) as WindowManager).defaultDisplay.getSize(point)
    //方屏另一个模版
    if (abs((point.x - point.y).toDouble()) < 60) {
        orientation = Configuration.ORIENTATION_UNDEFINED
    }

    return when (orientation) {
        Configuration.ORIENTATION_PORTRAIT -> R.layout.activity_wordsearch_game_old
        Configuration.ORIENTATION_LANDSCAPE ->  R.layout.activity_wordsearch_landlayout_game
        Configuration.ORIENTATION_UNDEFINED -> R.layout.activity_wordsearch_game
        else -> R.layout.activity_wordsearch_game_old
    }
}
